//
//  HWReadHabitView.m
//  嗨文
//
//  Created by 响当当 on 2019/1/19.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "HWReadHabitView.h"
#import "HWSegmentView.h"
#import "HWChangeSegmentView.h"

@interface  HWReadHabitView()
@property (nonatomic, strong)UILabel *titleLabel;//标题
@property (nonatomic, strong)UIButton *saveButton;//保存

@property (nonatomic, copy)NSArray *boyHabitArr;//男生爱好
@property (nonatomic, copy)NSArray *GirlHabitArr;//女生爱好

@property (nonatomic, strong)NSMutableArray *boyHabitNumArr;//男生爱好类型
@property (nonatomic, strong)NSMutableArray *GirlHabitNumArr;//女生爱好类型

@property (nonatomic, strong)NSString *GirlOrBoyHabitStr;//女生或者男生

@property (nonatomic, strong)HWSegmentView *segmentView;//标签
@property (nonatomic, strong)HWChangeSegmentView *hwChangeSegmentView;//分类
@property (nonatomic, strong)HWBoyBookView *boyBookView;//男生喜好
@property (nonatomic, strong)HWGirlBookView *girlBookView;//女生喜好

@end

@implementation HWReadHabitView

- (NSMutableArray *)boyHabitNumArr
{
    if (!_boyHabitNumArr)
    {
        _boyHabitNumArr = [[NSMutableArray alloc]init];
    }
    return _boyHabitNumArr;
}

- (NSMutableArray *)GirlHabitNumArr
{
    if (!_GirlHabitNumArr)
    {
        _GirlHabitNumArr = [[NSMutableArray alloc]init];
    }
    return _GirlHabitNumArr;
}

- (NSArray *)boyHabitArr
{
    if (!_boyHabitArr)
    {
//        _boyHabitArr = @[@"古代言情",@"古代言情",@"古代言情",@"古代言情",@"古代言情",@"古代言情",@"古代言情",@"古代言情",@"古代言情"];
        _boyHabitArr = [[NSArray alloc]init];
    }
    return _boyHabitArr;
}

- (NSArray *)GirlHabitArr
{
    if (!_GirlHabitArr)
    {
//        _GirlHabitArr = @[@"古代言情",@"古代言情",@"古代言情",@"古代言情",@"古代言情",@"古代言情",@"古代言情",@"古代言情",@"古代言情"];
        _GirlHabitArr = [[NSArray alloc]init];
    }
    return _GirlHabitArr;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel)
    {
        _titleLabel = [HWUtilties creatLabelWithFrame:CGRectMake(0,35*kUIScaleBasePlus,SCREEN_WIDTH,60*kUIScaleBasePlus)
                                         textAligment:NSTextAlignmentCenter
                                             textClor:LCHexColor(0x4C5159)
                                             textFont:24*kUIScaleBasePlus];
        NSString *str = @"选择你的阅读偏好\n这有助于我们推荐最适合你口味的作品";
        _titleLabel.attributedText = [str addAttriButedString:str
                                                        Color:LCHexColor(0x474B55)
                                                         Font:[UIFont systemFontOfSize:14*kUIScaleBasePlus]
                                                         rang:NSMakeRange(8,str.length-8)];
        
        _titleLabel.numberOfLines = 0;
    }
    return _titleLabel;
}

- (HWSegmentView *)segmentView
{
    if (!_segmentView)
    {
        _segmentView = [[HWSegmentView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(self.titleLabel.frame)+28*kUIScaleBasePlus,SCREEN_WIDTH,15*kUIScaleBasePlus)];
        WeakSelf(self)
        _segmentView.getBackBoyAndGirlBlock = ^(NSInteger num) {
            weakSelf.GirlOrBoyHabitStr = [NSString stringWithFormat:@"%ld",num];
        };
    }
    return _segmentView;
}

- (HWChangeSegmentView *)hwChangeSegmentView
{
    if (!_hwChangeSegmentView)
    {
        _hwChangeSegmentView = [[HWChangeSegmentView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(self.segmentView.frame),SCREEN_WIDTH,316*kUIScaleBasePlus)];
        WeakSelf(self)
        _hwChangeSegmentView.segemtChangeBlock = ^(NSInteger num) {
            [weakSelf changeSgment:num];
        };
    }
    return _hwChangeSegmentView;
}

- (HWBoyBookView *)boyBookView
{
    if (!_boyBookView)
    {
        _boyBookView = [[HWBoyBookView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(self.segmentView.frame),SCREEN_WIDTH,316*kUIScaleBasePlus)];
        _boyBookView.backgroundColor = [UIColor whiteColor];
        _boyBookView.hidden = YES;
        WeakSelf(self)
        _boyBookView.boyHabitNumBlock = ^(NSInteger num) {
            [weakSelf getBoyHabitNum:num];
        };
    }
    return _boyBookView;
}

- (HWGirlBookView *)girlBookView
{
    if (!_girlBookView)
    {
        _girlBookView = [[HWGirlBookView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(self.segmentView.frame),SCREEN_WIDTH,316*kUIScaleBasePlus)];
        _girlBookView.backgroundColor = [UIColor whiteColor];
        _girlBookView.hidden = YES;
        WeakSelf(self)
        _girlBookView.girlHabitNumBlock = ^(NSInteger num) {
            [weakSelf getGirlHabitNum:num];
        };
    }
    return _girlBookView;
}

- (UIButton *)saveButton
{
    if (!_saveButton)
    {
        _saveButton = [HWUtilties creatButtonWithFrame:CGRectMake(123*kUIScaleBasePlus,CGRectGetMaxY(self.hwChangeSegmentView.frame)-50*kUIScaleBasePlus,130*kUIScaleBasePlus,44*kUIScaleBasePlus)
                                          textAligment:NSTextAlignmentCenter
                                                  text:@"保存"
                                              textClor:LCHexColor(0xffffff)
                                             buttonBGC:LCHexColor(0xF54337)
                                              textFont:17*kUIScaleBasePlus
                                                target:self
                                                method:@selector(saveDate)];
        _saveButton.layer.masksToBounds = YES;
        _saveButton.layer.cornerRadius = _saveButton.frame.size.height/2;
        _saveButton.hidden = YES;
    }
    return _saveButton;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self addSubview:self.titleLabel];
        [self addSubview:self.segmentView];
        [self addSubview:self.hwChangeSegmentView];
        [self addSubview:self.boyBookView];
        [self addSubview:self.girlBookView];
        [self addSubview:self.saveButton];

    }
    return self;
}

- (void)changeSgment:(NSInteger)num
{
    self.saveButton.hidden = NO;
    
    WeakSelf(self)
    switch (num)
    {
        case 0:
        {
            
            [UIView animateWithDuration:0.2 animations:^{
                
            } completion:^(BOOL finished) {
                weakSelf.boyBookView.hidden = NO;
                [weakSelf.segmentView changeColor:num+1];
            }];
        }
            break;
        case 1:
        {
            [UIView animateWithDuration:0.2 animations:^{
                weakSelf.girlBookView.hidden = NO;
                [weakSelf.segmentView changeColor:num+1];
            }];
        }
            break;
    }
}

#pragma mark - 保存数据
- (void)saveDate
{
    
    NSInteger num = 0;
    NSDictionary *prarmers = [[NSDictionary alloc]init];
    
    NSArray *arr = [[NSArray alloc]init];
    
    if ([self.GirlOrBoyHabitStr isEqualToString:@"0"])
    {
        num = self.GirlHabitNumArr.count;
        arr = self.GirlHabitNumArr;
    }
    else
    {
        num = self.boyHabitNumArr.count;
        arr = self.boyHabitNumArr;
    }
    
    if (num == 0)
    {
        [SVProgressHUD showWithStatus:@"请选择个人爱好"];
        return;
    }
    
    
    switch (num)
    {

        case 1:
        {
            prarmers = @{
                         @"access_token" : current_User.token,
                         @"readTaste" : self.GirlOrBoyHabitStr,
                         @"tasteSeries[]" : arr[0]
                             };
        }
            break;
        case 2:
        {
            prarmers = @{
                         @"access_token" : current_User.token,
                         @"readTaste" : self.GirlOrBoyHabitStr,
                         @"tasteSeries[]" : [NSString stringWithFormat:@"%@,%@",arr[0],arr[1]]
                         };
        }
            break;
        case 3:
        {
            prarmers = @{
                         @"access_token" : current_User.token,
                         @"readTaste" : self.GirlOrBoyHabitStr,
                         @"tasteSeries[]" : [NSString stringWithFormat:@"%@,%@,%@",arr[0],arr[1],arr[2]]
                         };
        }
            break;
        case 4:
        {
            prarmers = @{
                         @"access_token" : current_User.token,
                         @"readTaste" : self.GirlOrBoyHabitStr,
                         @"tasteSeries[]" : [NSString stringWithFormat:@"%@,%@,%@,%@",arr[0],arr[1],arr[2],arr[3]]
                         };
        }
            break;
        case 5:
        {
            prarmers = @{
                         @"access_token" : current_User.token,
                         @"readTaste" : self.GirlOrBoyHabitStr,
                         @"tasteSeries[]" : [NSString stringWithFormat:@"%@,%@,%@,%@,%@",arr[0],arr[1],arr[2],arr[3],arr[4]]
                         };
        }
            break;
        case 6:
        {
            prarmers = @{
                         @"access_token" : current_User.token,
                         @"readTaste" : self.GirlOrBoyHabitStr,
                         @"tasteSeries[]" : [NSString stringWithFormat:@"%@,%@,%@,%@,%@,%@",arr[0],arr[1],arr[2],arr[3],arr[4],arr[5]]
                         };
        }
            break;
        case 7:
        {
            prarmers = @{
                         @"access_token" : current_User.token,
                         @"readTaste" : self.GirlOrBoyHabitStr,
                         @"tasteSeries[]" : [NSString stringWithFormat:@"%@,%@,%@,%@,%@,%@,%@",arr[0],arr[1],arr[2],arr[3],arr[4],arr[5],arr[6]]
                         };
        }
            break;
        case 8:
        {
            prarmers = @{
                         @"access_token" : current_User.token,
                         @"readTaste" : self.GirlOrBoyHabitStr,
                         @"tasteSeries[]" : [NSString stringWithFormat:@"%@,%@,%@,%@,%@,%@,%@,%@",arr[0],arr[1],arr[2],arr[3],arr[4],arr[5],arr[6],arr[7]]
                         };
        }
            break;
        case 9:
        {
            prarmers = @{
                         @"access_token" : current_User.token,
                         @"readTaste" : self.GirlOrBoyHabitStr,
                         @"tasteSeries[]" : [NSString stringWithFormat:@"%@,%@,%@,%@,%@,%@,%@,%@,%@",arr[0],arr[1],arr[2],arr[3],arr[4],arr[5],arr[6],arr[7],arr[8]]
                         };
        }
            break;
        default:
            break;
    }
    
    
    
    
    WeakSelf(self)
    [NetWorkingManager postRequestUrl:FULL_WEB_URL(KAPI_SAVEBOYHABIT) withPramater:prarmers sucess:^(id responseObject) {
        
        [kUserDefaults setObject:@"1" forKey:@"habbit"];
        [kUserDefaults synchronize];
        
        if (weakSelf.getBackSaveHabbitBlock)
        {
            weakSelf.getBackSaveHabbitBlock();
        }
        
    } failure:^(NSError *error) {
        DISMISS_SVP(1.0f);
        [SVProgressHUD showErrorWithStatus:@"保存失败，请检查网络是否良好"];
        DISMISS_SVP(1.0f);
    }];
}

- (void)getBoyHabitList:(NSArray *)arr;
{
    self.boyHabitArr = [NSArray arrayWithArray:arr];
    [self.boyBookView creatUI:arr];
}

- (void)getGirlHabitList:(NSArray *)arr
{
    self.GirlHabitArr = [NSArray arrayWithArray:arr];
    [self.girlBookView creatUI:arr];
}

- (void)getBoyHabitNum:(NSInteger )num
{
    if (self.boyHabitNumArr.count == 0)
    {
        [self.boyHabitNumArr addObject:[NSString stringWithFormat:@"%ld",num]];
    }
    else
    {
        [self.boyHabitNumArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSString *str = (NSString *)obj;
            if (str.integerValue == num)
            {
                [self.boyHabitNumArr removeObjectAtIndex:idx];
            }
        }];
        
        [self.boyHabitNumArr addObject:[NSString stringWithFormat:@"%ld",num]];
    }
}

- (void)getGirlHabitNum:(NSInteger )num
{
    if (self.GirlHabitNumArr.count == 0)
    {
        [self.GirlHabitNumArr addObject:[NSString stringWithFormat:@"%ld",num]];
    }
    else
    {
        [self.GirlHabitNumArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSString *str = (NSString *)obj;
            if (str.integerValue == num)
            {
                [self.GirlHabitNumArr removeObjectAtIndex:idx];
            }
        }];
        
        [self.GirlHabitNumArr addObject:[NSString stringWithFormat:@"%ld",num]];
    }
}
@end
